#include "smmovebutton.h"

smMoveButton::smMoveButton(QWidget *parent) : QPushButton(parent) {}

void smMoveButton::mousePressEvent(QMouseEvent *event) {
  if (event->button() == Qt::LeftButton) {
    startPos = event->pos();
  }
}

void smMoveButton::mouseMoveEvent(QMouseEvent *event) {
  stopPos = event->pos();
  int posX = stopPos.x() - startPos.x();
  int posY = stopPos.y() - startPos.y();

  if ((this->x() + posX <= parentWidget()->width() - this->width()) &&
      (this->y() + posY <= parentWidget()->height() - this->height()) &&
      this->x() + posX >= 0 && this->y() + posY >= 0) {
    this->move(this->x() + posX, this->y() + posX);
  }
}

void smMoveButton::keyPressEvent(QKeyEvent *event) {
  if (event->key() == Qt::Key_Left) {
    if (this->x() - 4 >= 0) {
      this->move(this->x() - 4, this->y());
    }
  } else if (event->key() == Qt::Key_Right) {
    if (parentWidget()->width() - this->width() - this->x() - 4 >= 0) {
      this->move(this->x() + 4, this->y());
    }
  } else if (event->key() == Qt::Key_Up) {
    if (this->y() - 4 >= 0) {
      this->move(this->x(), this->y() - 4);
    }
  } else if (event->key() == Qt::Key_Down) {
    if (parentWidget()->height() - this->height() - this->y() - 4 >= 0) {
      this->move(this->x(), this->y() + 4);
    }
  }
}
